package org.elepower.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.annotations.Param;
import org.elepower.dao.SystemConfigDao;
import org.elepower.pojo.PsRole;
import org.elepower.pojo.PsSysteamconfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/systemConfig")
public class SystemConfigController {

    @Autowired
    private SystemConfigDao systemConfigDao;

    @RequestMapping("/index")
    public String systemConfig() throws Exception{
        return "system/systemConfig/index";
    }

    @RequestMapping("/selectInfo")
    @ResponseBody
    public Map<String,Object> selectInfo(@RequestParam(defaultValue = "1" )Integer page , @RequestParam(defaultValue = "10") Integer limit) throws Exception{
        Map<String,Object> map = new HashMap<>();

        Page<PsSysteamconfig> pager = PageHelper.startPage(page,limit);

        List<PsSysteamconfig> systeamconfigList = systemConfigDao.getAll();

        map.put("count",pager.getTotal());
        map.put("data",systeamconfigList);
        map.put("code",0);
        map.put("msg","");
        return map;
    }

    @RequestMapping("/form")
    public String form() throws Exception{
        return "system/systemConfig/form";
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> add(PsSysteamconfig psSysteamconfig) throws Exception{
        Map<String,Object> map = new HashMap<>();

        if(psSysteamconfig.getIsStart() == null){
            psSysteamconfig.setIsStart(0);
        }

        systemConfigDao.add(psSysteamconfig);
        map.put("result",true);
        return map;
    }

    @RequestMapping(value ="/del",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> del(Integer configId) throws Exception{
        Map<String,Object> map = new HashMap<>();
        systemConfigDao.del(configId);
        map.put("result",true);
        return map;
    }

    @RequestMapping("/get")
    @ResponseBody
    public PsSysteamconfig get(Integer id) throws Exception{
        return systemConfigDao.get(id);
    }

    @RequestMapping("/update")
    @ResponseBody
    public Map<String,Object> update(PsSysteamconfig psSysteamconfig) throws Exception{
        Map<String,Object> map = new HashMap<>();

        if(psSysteamconfig.getIsStart() == null){
            psSysteamconfig.setIsStart(0);
        }

        systemConfigDao.update(psSysteamconfig);
        map.put("result",true);
        return map;
    }

    @RequestMapping("/getBugType")
    @ResponseBody
    public List<PsSysteamconfig> getBugType() throws Exception{
        return systemConfigDao.getBugType();
    }

    @RequestMapping("/getBugLevel")
    @ResponseBody
    public List<PsSysteamconfig> getBugLevel() throws Exception{
        return systemConfigDao.getBugLevel();
    }
}
